<?php
class Admin_NewsController extends Zendvn_Controller_Action{
	
	private $_arrParam;
	private $_mainAction;
	
	public function init(){
		$this->loadTemplate(TEMPLATE_PATH .'/admin');
		
		$this->view->headScript()->appendFile(TEMPLATE_URL .'/admin/js/form.js');
		$this->view->headScript()->appendFile(TEMPLATE_URL .'/admin/js/submit.js');
		$this->_arrParam = $this->_request->getParams();
		$this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller']. '/index';
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$tblNews = new Model_Table_News();		
		$this->view->Items = $tblNews->allNews();
	}
	public function addAction(){		
		$tblNews = new Model_Table_News();		
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateNews($this->_arrParam);
			if($validator->isValid() == true){
				//Hien thong bao loi
				$this->view->arrError = $validator->getErrors();
				$this->view->arrData = $validator->getData();
			}else{				
				$data = $validator->getData(); 
				$tblNews->addNew($data);
				$this->_redirect($this->_mainAction);
			}
		}	
	}
	public function deleteAction(){
		$tblNews = new Model_Table_News();
		$tblNews->deleteItem($this->_arrParam);
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function editAction(){
		$tblNews = new Model_Table_News();
		$this->view->Item = $tblNews->getNew($this->_arrParam);
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateNews($this->_arrParam);
			if($validator->isValid() == true){
				//Hien thong bao loi
				$this->view->arrError = $validator->getErrors();
				$this->view->arrData = $validator->getData();
			}else{
				$data = $validator->getData(); 
				$tblNews->editItem($data);
				$this->_redirect($this->_mainAction);
			}
			
		}
	}
	public function statusAction(){
		$tblNews = new Model_Table_News();
		$tblNews->changeStatus($this->_arrParam);
		
		$status = $this->_arrParam['status'];
		$id = $this->_arrParam['id'];
		
		$string = '<input type="hidden" id="status" value="'.$status.'" />';
		if($status == 1) $string .= '<img src="'.APPLICATION_URL.'/public/templates/admin/images/ball_green_16.png" class="block clickable" alt="" onclick="javascript:changeStatus('.$status.','.$id.');"/>';
		if($status == 0) $string .= '<img src="'.APPLICATION_URL.'/public/templates/admin/images/ball_red_16.png" class="block clickable" alt="" onclick="javascript:changeStatus('.$status.','.$id.');"/>';
		echo $string;
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}
}